import matplotlib.pyplot as plt
import numpy as np
import statistics
import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic')

plt.rcParams.update({'font.size': 20})

def step_(ax, data, label):
    # n, bins, patches = ax.hist(data, 90, histtype='step', density=True, linewidth=3,
                        #    cumulative=True, label=label)

    # patches[0].set_xy(patches[0].get_xy()[:-1])
    # ax.plot(np.sort(data), np.linspace(0, 1, len(data), endpoint=False), linewidth=3)
    ax.step(np.sort(data), np.arange(1, len(data)+1) / float(len(data)), where='post', label=label, linewidth=3)


def memory():
    ts = []
    for line in open('overhead/st/memory.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts.append(float(arr[1])/1000000)
    ts1 = []
    for line in open('overhead/i2/memory.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts1.append((float(arr[1]))/1000000)
    # ts1=ts1+ts1
    # ts1.(100)
    print(ts1)
    ts2 = []
    for line in open('overhead/fattree/fattree_memory.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts2.append((float(arr[1]))/1000000)
    ts3 = []
    for line in open('overhead/fb/fb_memory.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts3.append((float(arr[1]))/1000000)


    fig, ax = plt.subplots(figsize=(6,3.5))
    step_(ax, ts, 'Stanford')
    step_(ax, ts1, 'Internet2')
    step_(ax, ts2, 'Fattree')
    step_(ax, ts3, 'LONet')
    ax.grid()
    ld = ax.legend(loc='lower left', fontsize='small')
    ld.get_frame().set_alpha(0.8)
    ax.set_xscale('log')
    ax.set_xlabel('Memory (MB)')
    # plt.show()
    plt.savefig('memory.png',bbox_inches='tight')
    plt.tight_layout()
    print(max(ts))
    print(max(ts1))
    print(max(ts2))
    print(max(ts3))

def cib():
    ts = []
    for line in open('overhead/st/cib num.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts.append(int(arr[1]))
    ts1 = []
    for line in open('overhead/i2/cib num.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts1.append(int(arr[1]))
    ts2 = []
    for line in open('overhead/fattree/cib num.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts2.append(int(float(arr[1])))
    # print(ts2)
    ts3 = []
    for line in open('overhead/fb/cib num.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts3.append(float(arr[1]))
    

    fig, ax = plt.subplots(figsize=(8,5))
    step_(ax, ts, 'Stanford')
    step_(ax, ts1, 'Internet2')
    step_(ax, ts2, 'Fattree')
    step_(ax, ts3, 'LONet')
    ax.grid()
    ax.legend()
    ax.set_xscale('log')
    # ax.set_xlabel('#Entries in CIB')
    # plt.show()
    plt.savefig('cib.png',bbox_inches='tight')
    plt.tight_layout()

def msg():
    ts = []
    for line in open('overhead/st/msg size.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts.append(int(arr[1])/1000)
    print(max(ts))
    ts1 = []
    for line in open('overhead/i2/msg size.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts1.append(int(arr[1])/1000)
    print(max(ts1))
    ts2 = []
    for line in open('overhead/fattree/msg size.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts2.append(int(arr[1])/1000)
    print(max(ts2))
    ts3 = []
    for line in open('overhead/fb/msg size.txt'):
        if 'total' in line:
            continue
        arr = line.split(' ')
        ts3.append(int(arr[1])/1000)
    print(max(ts3))

    fig, ax = plt.subplots(figsize=(6,3.5))
    step_(ax, ts, 'Stanford')
    step_(ax, ts1, 'Internet2')
    step_(ax, ts2, 'Fattree')
    step_(ax, ts3, 'LONet')
    # ax.step(np.sort(ts), np.arange(1, len(ts)+1) / float(len(ts)), where='post', label='Internet2')
    # ax.step(np.sort(ts1), np.arange(1, len(ts1)+1) / float(len(ts1)), where='post', label='Stanford')
    ax.grid()
    ax.legend(loc='lower right', fontsize='small')
    
    ax.set_xscale('log')
    ax.set_xlabel('Message size (KB)')
    # plt.show()
    plt.savefig('msg.png',bbox_inches='tight')
    plt.tight_layout()

# memory()
# cib()
msg()